package com.bxj.wxtt.controller;

import com.bxj.wxtt.api.domain.*;
import com.bxj.wxtt.api.service.IAdvContentDubboService;
import com.bxj.wxtt.api.service.IAdvDubboService;
import com.bxj.wxtt.api.service.IAdvExDubboService;
import com.ifeng.matrix.common.domain.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by baixiujiang on 17/10/15.
 */
@Controller
@RequestMapping(value = "/article")
public class ArticleController {

    @Resource
    IAdvExDubboService advExDubboService;
    @Resource
    IAdvDubboService advDubboService;
    @Resource
    IAdvContentDubboService advContentDubboService;

    @RequestMapping(value = {"/get_list"}, method = RequestMethod.GET)
    public ResponseEntity get_list(@RequestParam(required = true)int channelId, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int count) {
        Adv adv = new Adv();
        adv.setChannelId(channelId);
        adv.setOrderByClause("date_time desc");
        adv.setPageIndex(page);
        adv.setPageSize(count);
        PageResult<Adv> advPage = advDubboService.getAdvPage(adv);

        List<AdvBO> advBOList = convertToAdvBO(advPage.getResult());
        return returnSuccessResult(advBOList, (int) advPage.getCount());
    }

    private List<AdvBO> convertToAdvBO(List<Adv> list) {
        List<AdvBO> advBOList = new ArrayList<>();
        for (Adv advResult : list) {
            AdvBO advBO = new AdvBO();
            BeanUtils.copyProperties(advResult, advBO);
            List<String> imgList = new ArrayList<>();
            if (!StringUtils.isEmpty(advResult.getImg1()))
                imgList.add(advResult.getImg1());
            if (!StringUtils.isEmpty(advResult.getImg2()))
                imgList.add(advResult.getImg2());
            advBO.setImgList(imgList);
            advBOList.add(advBO);
        }
        return advBOList;
    }

    @RequestMapping(value = {"/get_detail"}, method = RequestMethod.GET)
    public ResponseEntity get_detail(String articleId) {
        Adv adv = advDubboService.selectById(articleId);
        AdvContentBO advContentBO = new AdvContentBO();
        BeanUtils.copyProperties(adv, advContentBO);


        AdvContent queryAdvContent = new AdvContent();
        queryAdvContent.setAdvId(adv.getArticleId());
        queryAdvContent.setOrderByClause("num");
        StringBuffer content = new StringBuffer();
        List<AdvContent> advContentList = advContentDubboService.getAdvContentList(queryAdvContent);
        for (AdvContent advContent : advContentList) {
            content.append(advContent.getContent());
        }
        AdvEx advEx = new AdvEx();
        advEx.setRecommandId(articleId);
        List<Adv> advRelationList = advExDubboService.getAdvRelationList(advEx);
        advContentBO.setArticleList(convertToAdvBO(advRelationList));
        advContentBO.setContent(content.toString());
        return returnSuccessResult(advContentBO);
    }

    private <T> ResponseEntity returnSuccessResult(T t) {
        return returnSuccessResult(t, null);
    }
    private <T> ResponseEntity returnSuccessResult(T t, Integer count) {
        JsonResult result = new JsonResult();
        result.setCode(100);
        result.setMsg("success");
        result.setCount(count);
        result.setData(t);
        return new ResponseEntity(result, HttpStatus.OK);
    }

}
